<?php
namespace app\ClubAdmin\controller;

use think\Controller;
use think\Session;
use think\Db;
class ClubController extends Controller
{
    public function _initialize()
    {
        if (!Session::has('clubid'))
        {
            $this->redirect('index/index/index');
        }
    }

    public function index()
    {
       // 查询状态为1的用户数据 并且每页显示5条数据
         $item = Db::name('clubs')->where('cid',Session::get('clubid'))->find();
         $num=db('focus')->where('cid',$item['cid'])->count();
                    $item['num']=$num;
                    $num1=db('lessons')->where('cid',$item['cid'])->count();
                    $item['num1']=$num1;
                    $num2=db('videos')->where('cid',$item['cid'])->count();
                    $item['num2']=$num2;
                    $num3=db('focus')->where('cid',$item['cid'])->count();
                    $item['num3']=$num3;
                   
                  
   
        // 把分页数据赋值给模板变量list
       $this->assign('vo', $item);
        // 渲染模板输出
          return $this->fetch('club');
        
    }

    public function edit(){
      $cid=input('cid');
      $list=Db::name('clubs')->where('cid',$cid)->find();
      echo(json_encode($list)) ;

    }

    public function update(){
       $picname=input('cpic');
       $cid=input('cid');
        //dump($cid);
        //exit;
       $file = request()->file('cpic');
         $pic="";
    // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->rule('uniqid')->validate(['size'=>3000000,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'static'. DS . 'img'.DS.'clubimg');
            //dump($file);exit;
            if($info){
                // 成功上传后 获取上传信息
                $pic=$info->getFilename();
            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }
        $data=[
            'cname'=>input('cname'),
            'cintro' =>input('cintro'),
            'ctel'=>input('ctel'),
            'copentime'=>input('copentime'),
            'cpic'=>empty($pic)?$picname:$pic,
            'caddress'=>input('caddress'),
             'clatitude'=>input('clatitude'),
            'clongitude'=>input('clongitude'),
            
        ]; 
        // dump($data);exit;
       if( Db::name('clubs')->where('cid',$cid)->update($data)){
          $this->success('更新俱乐部信息成功！');

        }
        else
          $this->error('更新俱乐部信息失败');

    }

    public function editpass()
    {
        return $this->fetch();
    }

    public function confirm_editpass()
    {
        $clubadmin=\db('clubs')->find(Session::get('clubid'));
        if($clubadmin['cpwd']==input('oldPassword'))
        {
            $clubadmin['cpwd']=input('newPassword');
            \db('clubs')->update($clubadmin);
            $this->success('修改成功');
        }
        else
        {
            $this->error('修改失败，与原密码不符');
        }

    }

}